<?php
/**
 * Model class
 *
 * @access 	public
 * @author 	Salvatore Borrelli
 * @copyright	ResMania 2009 all rights reserved.
 * @version	1.0
 * @link		http://developer.resmania.com/api
 * @since  	11-2011
 */
class RM_UnitGracePriceBandsConfig extends RM_Model
{
    /** Database meta information */
    const F_UNIT_ID = "unit_id";
    const F_NAME = "name";
    const F_VALUE = "value";
    const V_DATE_BASED_ENABLED = "date_based_enabled";
    const V_DEFAULT_START_TIME = "default_start_time";
    const V_DEFAULT_END_TIME = "default_end_time";
    
    const DEFAULT_START_HOURS = "default_start_hours";
    const DEFAULT_START_MINUTES = "default_start_minutes";
    const DEFAULT_END_HOURS = "default_end_hours";
    const DEFAULT_END_MINUTES = "default_end_minutes";
    
    protected $_name = 'rm_unit_grace_price_bands_config';
    
    /**
     * Return the value of the date-based configuration parameter for a given unit, or the default
     *  configuration value if the unit specific configuration is not available
     * @param [string] unitId the unit identifier
     * @return [int] 0 if the date-based configuration is disabled, 1 if it is enabled
     */
    public function getDateBasedEnabled($unitId) {
        $value = 0;
        try {
            $res = $this->fetchValueByUnit($unitId, self::V_DATE_BASED_ENABLED);
            if (!empty($res)) {
                $value = intval($res);
            }
        }
        catch(Exception $e) {
            RM_Log::toLog("failed to get date-based configuration parameter for unit $unitId", $e);
        }
        return $value;
    }
    /**
     * Return the value of the time-selection configuration parameters for a given unit, or the default
     *  configuration value if the unit specific configuration is not available
     * @param [string] unitId the unit identifier
     * @return [array] the time selection configuration parameters
     */
    public function getTimeSelectionInfo($unitId) {
        $timeSelInfo = array();
        try {
            // gets the default start time for reservations
            $stm = $this->fetchValueByUnit($unitId, self::V_DEFAULT_START_TIME);
            $tm = strtotime($stm);
            $dt = getdate($tm);
            $hours = $dt['hours'];
            $mins = $dt['minutes'];
            if (strlen($mins)<2) {
                $mins = "0$mins";
            }
            $timeSelInfo[self::V_DEFAULT_START_TIME] = $hours.":".$mins;
            // gets the default end time for reservations
            $stm = $this->fetchValueByUnit($unitId, self::V_DEFAULT_END_TIME);
            $tm = strtotime($stm);
            $dt = getdate($tm);
            $hours = $dt['hours'];
            $mins = $dt['minutes'];
            if (strlen($mins)<2) {
                $mins = "0$mins";
            }
            $timeSelInfo[self::V_DEFAULT_END_TIME] = $hours.":".$mins;
        }
        catch(Exception $e) {
            RM_Log::toLog("failed to get time-selection configuration parameter for unit $unitId", $e);
        }
        return $timeSelInfo;
    }
    /**
     * Delete all unit price config preferences
     *
     * @param int $unitID
     * @return int          The number of rows deleted.
     */
    function deleteByUnit($unitID)
    {
        return $this->delete("unit_id='$unitID'");
    }
    /**
     * Returns rows that are EXACTLY in rm_unit_daily_prices_config table assigned for input unit
     *
     * @param RM_Unit_Row $unit
     * @return void
     */
    function getByUnit(RM_Unit_Row $unit)
    {
        return $this->fetchAll($this->select()->where('unit_id=?', $unit->id));
    }
    /**
     * Return all information about unit price config, if there is no value
     * for some field it will return the default one
     *
     * @param $unitID Int - unit primary key value
     */
    function fetchByUnit($unitID)
    {
        $sql = "
            SELECT                
                pc.name as name,
                pc.value as default_value,
                pc.metainfo as metainfo,
                upc.value as unit_value,
                '$unitID' as unit_id
            FROM
                rm_grace_price_bands_config pc
            LEFT OUTER JOIN
                rm_unit_grace_price_bands_config upc ON upc.name=pc.name AND upc.unit_id=$unitID
        ";
        $result = $this->_getBySQL($sql);
        return $result;
    }

    /**
     * Fetch units by config value
     */
    function fetchUnitsByValue($value){
        $sql = "
            SELECT
                unit_id, value
            FROM
                rm_unit_grace_price_bands_config
            WHERE
                name='$value'
        ";
        $result = $this->_getBySQL($sql);
        return $result;
    }

    /**
     * Returns unit config value by name.
     *
     * @param int $unitID - unit primary key
     * @param string $name - config name
     * @return mixed - if unit value set, returns unit value. Otherwise default value.
     */
    function fetchValueByUnit($unitID, $name)
    {
        $sql = "
            SELECT                
                pc.value as default_value,
                upc.value as unit_value
            FROM
                rm_grace_price_bands_config pc
            LEFT OUTER JOIN
                rm_unit_grace_price_bands_config upc ON upc.name=pc.name AND upc.unit_id=$unitID
            WHERE
                pc.name='$name'
        ";

        $result = $this->_getBySQL($sql)->current();
        if (isset($result->unit_value) && $result->unit_value !== null) {
            return $result->unit_value;
        } elseif (isset($result->default_value)) {
            return $result->default_value;
        }
    }
}
